﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Net;
using System.Web;
using System.IO;
using System.Xml; 


namespace USBReader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            matchDriveLetterWithSerial();
            foreach (string item in _serialNumber)
            {
                txtDriver.Text += item+"\t";
                if (item.TrimEnd('\t').Equals("3F3BB353"))
                {
                    MessageBox.Show("认证成功！");
                }
            }
        }
        private List<string> _serialNumber = new List<string>();

        /// <summary>
        /// 调用这个函数将本机所有U盘序列号存储到_serialNumber中
        /// </summary>
        private void matchDriveLetterWithSerial()
        {

            //var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
            //foreach (ManagementObject dm in searcher.Get())
            //{
            //    getValueInQuotes(dm["Dependent"].ToString());
            //    diskArray = getValueInQuotes(dm["Antecedent"].ToString()).Split(',');
            //    driveNumber = diskArray[0].Remove(0, 6).Trim();
            //    var disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
            //    foreach (ManagementObject disk in disks.Get())
            //    {
            //        if (disk["Name"].ToString() == ("\\\\.\\PHYSICALDRIVE" + driveNumber) & disk["InterfaceType"].ToString() == "USB")
            //        {
            //            _serialNumber.Add(parseSerialFromDeviceID(disk["PNPDeviceID"].ToString()));
            //        }
            //    }
            //}
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT DeviceID FROM Win32_USBHub");
            // txtDriver.Text += wmi_HD["DeviceID"].ToString();
            _serialNumber.Clear();
            foreach (ManagementObject wmi_HD in searcher.Get())
            {
                //string[] Vids = wmi_HD["DeviceID"].ToString().Split('\\');
                //_serialNumber.Add(Vids[2]);
                string Vid = wmi_HD["DeviceID"].ToString();//.Split('\\');
                _serialNumber.Add(Vid);
            }

    //        For Each objItem In colItems
    //    a = objItem.DeviceID  'U盘识别为:USB\VID_09A6&PID_800\20040418154911-00，故用VID判别
    //    If a Like "*VID*" Then b = Split(a, "\"): MsgBox b(UBound(b))
    //    '上句亦可:If InStr(a, "VID") Then b = Split(a, "\"): MsgBox b(UBound(b))
    //Next

        }
        private static string parseSerialFromDeviceID(string deviceId)
        {
            var splitDeviceId = deviceId.Split('\\');
            var arrayLen = splitDeviceId.Length - 1;
            var serialArray = splitDeviceId[arrayLen].Split('&');
            var serial = serialArray[0];
            return serial;
        }

        private static string getValueInQuotes(string inValue)
        {
            var posFoundStart = inValue.IndexOf("\"");
            var posFoundEnd = inValue.IndexOf("\"", posFoundStart + 1);
            var parsedValue = inValue.Substring(posFoundStart + 1, (posFoundEnd - posFoundStart) - 1);
            return parsedValue;
        }

        

    }
}
